/**
 * 人员查询
 */
(function(){
	var employeeParam = {
		/**
		 * 初始化
		 */
		initemployee:function(){
			
			//调用后台接口，取得人员信息
			//转圈
			common.showLoading();
			var data={
					"pk_user":window.localStorage.userid,
					"pageSize":common.pageSize,
					"pageNum":common.currentPage,
					"params":window.sessionStorage.searchKmKey
					};
			var url = "/shellUser/queryAllUser";
			common.interfaceAjaxFunction(url,data,employeeParam.showemployeeSuccess);

		},
	
		/**
		 * 查询回调函数
		 */
		showemployeeSuccess:function(json){
			//成功时候
	    	if(json.status == "0"){
	    		employeeParam.showemployee(json);
	    	}else{
	    		//给出信息提示
	    		common.show_error(json.message);
	    	}
	    	//转圈提示去除
	    	common.hideLoading();
		},
		
		//页面拼写表格
		showemployee:function(json){
			var jsondata=json.data;
			$("#tbody").empty();
			if(json.data.length > 0){
				for(var i=0;i<jsondata.length;i++){
					//用户id
					var pk_user=jsondata[i].pk_user;
					//用户编码
					var user_code=jsondata[i].user_code;
					//用户名字
					var user_name=jsondata[i].user_name;
					//所属部门
					var tr_html = '<tr onclick="setCheck(this)">'+
		            '<td ><input type="checkbox" name="'+user_name+'" onclick="setCB_Check(this)"  class="subcheck" id="subcheck'+pk_user+'" value="'+pk_user+'"/></td>'+
		            '<td >'+user_code+'</td>'+
		            '<td >'+user_name+'</td>'+
		            '<td ></td>'+
		        '</tr>';
		        $("#tbody").append(tr_html);
				}
			}else{
				$("#tbody").append("<tr><td colspan='4'>暂无人员数据</td></tr>");
			}
			
			 var administrator=$("#administratoridInput").val();
			 if(administrator != undefined && administrator !=""){
				 var uidArray=administrator.split(",");
				 for(var k=0;k<uidArray.length;k++){
					 var inputid="#subcheck"+uidArray[k];
					 $(inputid).attr("checked", "checked");
				 }
			 }
			//产生分页结构
			common.getPageDivide(json.count,"pageDivideContainer",employeeParam.initemployee);
		},
		/**
		 * 事件绑定的处理
		 */
		initBindEven : function(){
			//选择人员
			$(".subcheck").unbind().on("click",function(){
				if($(this).prop("checked")){
					var id=$(this).val();
					var name=$(this).attr("name");
					var but_html = '<button class="deleteemp" id="deleteemp'+id+'" name="'+name+'" data="'+id+'">'+name+'<span class="glyphicon glyphicon-remove"></span>'+'</button>';
		        	 $("#but").append(but_html);
		        	 //删除人员
		        	 $(".deleteemp").on("click",function(){
		        		 $(this).remove();
		        		 var deleteempid=$(this).attr("id").substring(9);
		        		 var cid="subcheck"+deleteempid;
		        		 $("#"+cid).attr("checked",false);
		        		 //去掉几率的本地input中的值
			     		 var administratoridInput = $("#administratoridInput").val();
			     		 if(administratoridInput.indexOf(deleteempid + ",") >= 0){
			     			$("#administratoridInput").val(administratoridInput.replace(deleteempid + ",",""));
			     		 }else{
			     			$("#administratoridInput").val(administratoridInput.replace(deleteempid,""));
			     		 }
		    		});
            	}else{
            		var id=$(this).val();
            		$("#deleteemp"+id).remove();
            	}
			});
			//窗口的index
			var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
			//////提交
			$("#submit").unbind().on("click",function(){
				var knoedename="";
				var knode="";
					$("#but").find('Button').each(function(index) {
						 knoedename +=$(this).attr("data")+",";
					});
					$("#but").find('Button').each(function(index) {
						knode +=$(this).attr("name")+",";
					});
					 var pk_userid=knoedename.substring(0,knoedename.length-1);
					 var pk_username=knode.substring(0,knode.length-1);
					 //看看弹出类型
					 var popType = common.getURLparams().type;
					 if(popType != undefined && popType == "subpage"){
						var parindex = parent.parindex;
					    if (parindex) {
					    	var objname = parent.document.getElementById('layui-layer-iframe' + parindex).contentWindow.document.getElementById('administratorname');
					    	var objid = parent.document.getElementById('layui-layer-iframe' + parindex).contentWindow.document.getElementById('administratorid');
					    	if (objid) {
					    		objid.value =pk_userid;
					    	};
					    	
					    	if (objname) {
					    		objname.value =pk_username;
					    	};
					    	
					    };
					 }else{
						 parent.$('#administratorid').val(pk_userid);
						 parent.$('#administratorname').val(pk_username);
					 }
					 parent.layer.close(index); 
				
			});
			//取消弹出框	
			$("#cancel").unbind().on("click",function(){
					 parent.layer.close(index);
			});
			
			//搜索
			$("#search").unbind().on("click",function(){
				var user_name=$("#serchname").val();
				var data={
						"user_name":user_name,
						"pk_user":window.localStorage.userid,
						"pageSize":common.pageSize,
						"pageNum":common.currentPage,
						"params":window.sessionStorage.searchKmKey
				};
				var url = "/shellUser/queryAllUser";
				common.interfaceAjaxFunction(url,data,employeeParam.showemployeeSuccess);
			});
		},
		/**
		 * initSelectedPart 初始化已经选择部分
		 */
		initSelectedPart:function(){
			 //用友信息记录
			 var administratorid=parent.administratorid;
			 var administratorname=parent.administratorname;
			 //赋值隐藏项目
			 $("#administratoridInput").val(administratorid);
			 //显示已选择项目
			 if(administratorid != undefined && administratorid !=""){
				 var uidArray=administratorid.toString().split(",");
				 var unameArray=administratorname.toString().split(",");
				 for(var k=0;k<uidArray.length;k++){
					 var inputid="#subcheck"+uidArray[k];
					 $(inputid).attr("checked", "checked");
				 }
				 var but_html="";
				 for(var i=0;i<uidArray.length;i++){
					 but_html += '<button class="deleteemp" data="'+uidArray[i]+'" name="'+unameArray[i]+'" id="deleteemp'+uidArray[i]+'">'+unameArray[i]+'<span class="glyphicon glyphicon-remove"></span>'+'</button>';
				 }
		     	 $("#but").append(but_html);
		     	 //删除人员
		     	 $(".deleteemp").on("click",function(){
		     		 $(this).remove();
		     		 var deleteempid=$(this).attr("id").substring(9);
		     		 var cid="subcheck"+deleteempid;
		     		 $("#"+cid).attr("checked",false);
		     		 //去掉几率的本地input中的值
		     		 var administratoridInput = $("#administratoridInput").val();
		     		 if(administratoridInput.indexOf(deleteempid + ",") >= 0){
		     			$("#administratoridInput").val(administratoridInput.replace(deleteempid + ",",""));
		     		 }else{
		     			$("#administratoridInput").val(administratoridInput.replace(deleteempid,""));
		     		 }
		 		}); 
		     }
		}
	};


	$(document).ready(function(){
		employeeParam.initSelectedPart();
		employeeParam.initemployee();
		employeeParam.initBindEven();
	});
})();


function setCheck(ob){
	var rd=$(ob).children("td:eq(0)").children("input[type='checkbox']").prop("checked");
    if(rd==false){
       $(ob).children("td:eq(0)").children("input[type='checkbox']").prop("checked",true);
       var id= $(ob).children("td:eq(0)").children("input[type='checkbox']").val();
       var name=$(ob).children("td:eq(0)").children("input[type='checkbox']").attr("name");
       //看看是否已经记录
       var selectedIdStr = $("#administratoridInput").val();
       if(selectedIdStr == "" || selectedIdStr.indexOf(id) < 0){
    	   var buts_html = '<button class="deleteemp" id="deleteemp'+id+'" name="'+name+'" data="'+id+'">'+name+'<span class="glyphicon glyphicon-remove"></span>'+'</button>';
	  	   $("#but").append(buts_html);
	  	   //同时放到隐藏域中去
	  	   $("#administratoridInput").val(selectedIdStr + "," + id)
       }
  	 	//删除人员
  	 	$(".deleteemp").unbind().on("click",function(){
  		 $(this).remove();
  		 var deleteempid=$(this).attr("id").substring(9);
  		 var cid="subcheck"+deleteempid;
  		 $("#"+cid).attr("checked",false);
		});
    }else{
        $(ob).children("td:eq(0)").children("input[type='checkbox']").prop("checked",false);
        var id= $(ob).children("td:eq(0)").children("input[type='checkbox']").val();
        $("#deleteemp"+id).remove();
        //去掉几率的本地input中的值
		 var administratoridInput = $("#administratoridInput").val();
		 if(administratoridInput.indexOf(id + ",") >= 0){
			$("#administratoridInput").val(administratoridInput.replace(id + ",",""));
		 }else{
			$("#administratoridInput").val(administratoridInput.replace(id,""));
		 }
    }
}
function setCB_Check(ob){
	 var rd=$(ob).prop("checked");
     if(rd==true){
    	  $(ob).prop("checked",false);
     }else{
         $(ob).prop("checked",true);
     }
	
}